﻿using System;
using System.Net;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using MM7.MIME;
using System.Xml;
using System.Text.RegularExpressions;

namespace MM7 
{

	public class MessageClassType
	{
		private string _type = null;

		public override string ToString()
		{
			return _type;
		}

		public static MessageClassType Personal
		{
			get{ return new MessageClassType("Personal");}
		}

		public static MessageClassType Informational
		{
			get{ return new MessageClassType("Informational");}
		}
	
		public static MessageClassType Advertisement
		{
			get{ return new MessageClassType("Advertisement");}
		}

		public static MessageClassType Auto
		{
			get{ return new MessageClassType("Auto");}
		}

		public MessageClassType(string type)
		{
			if (string.Compare(type, "Personal") == 0)
			{
				_type = "Personal";
			}
			else if (string.Compare(type, "Informational") == 0)
			{
				_type = "Informational";
			}
			else if (string.Compare(type, "Advertisement") == 0)
			{
				_type = "Advertisement";
			}
			else if (string.Compare(type, "Auto") == 0)
			{
				_type = "Auto";
			}
			else
			{
				_type = "Informational";
			}
		}
	}

	public class PriorityType
	{
		private string _type = null;

		public override string ToString()
		{
			return _type;
		}

		public static PriorityType Normal
		{
			get{ return new PriorityType("Normal");}
		}

		public static PriorityType High
		{
			get{ return new PriorityType("High");}
		}
	
		public static PriorityType Low
		{
			get{ return new PriorityType("Low");}
		}

		public PriorityType(string type)
		{
			if (string.Compare(type, "Normal") == 0)
			{
				_type = "Normal";
			}
			else if (string.Compare(type, "High") == 0)
			{
				_type = "High";
			}
			else if (string.Compare(type, "Low") == 0)
			{
				_type = "Low";
			}
			else
			{
				_type = "Normal";
			}
		}
	}

	public class ChargedPartyType
	{
		private string _type = null;

		public override string ToString()
		{
			return _type;
		}

		public bool Equals(ChargedPartyType obj)
		{
			return string.Compare(_type, obj.ToString(), true) == 0;
		}

		public static ChargedPartyType Sender
		{
			get{ return new ChargedPartyType("Sender");}
		}

		public static ChargedPartyType Recipient
		{
			get{ return new ChargedPartyType("Recipient");}
		}
	
		public static ChargedPartyType Both
		{
			get{ return new ChargedPartyType("Both");}
		}

		public static ChargedPartyType Neither
		{
			get{ return new ChargedPartyType("Neither");}
		}

		public static ChargedPartyType ThirdParty
		{
			get{ return new ChargedPartyType("ThirdParty");}
		}

		public ChargedPartyType(string type)
		{
			if (string.Compare(type, "Sender") == 0)
			{
				_type = "Sender";
			}
			else if (string.Compare(type, "Recipient") == 0)
			{
				_type = "Recipient";
			}
			else if (string.Compare(type, "Both") == 0)
			{
				_type = "Both";
			}
			else if (string.Compare(type, "Neither") == 0)
			{
				_type = "Neither";
			}
			else if (string.Compare(type, "ThirdParty") == 0)
			{
				_type = "ThirdParty";
			}
			else
			{
				_type = "Neither";
			}
		}
	}

	public class MM7_Base
	{
		private string transactionIDValue;
		private string versionValue = "6.3.0";
		public string TransactionID
		{
			get{ return transactionIDValue;}
			set{ transactionIDValue = value;}
		}

		public string MM7Version
		{
			get{return versionValue;}
			set{versionValue = value;}
		}
	}

	public abstract class MM7_RS_Req : MM7_Base
	{
		internal abstract void GenInfo(byte[] bytes);
		internal static MM7_RS_Req Decode(byte[] bytes)
		{
			MM7_RS_Req req = null;
			XmlDocument  doc = new XmlDocument();
			try
			{
				MemoryStream memStream = new MemoryStream(bytes);
				doc.Load(memStream);
				memStream.Close();
				XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
				manager.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
				XmlNode body = doc.SelectSingleNode("//env:Body", manager);
				if (body == null)
				{
					return null;
				}
				XmlNode ddd  = body.LastChild;
				if (ddd == null)
				{
					return null;
				}
				else
				{
					manager.AddNamespace("mm7", ddd.NamespaceURI);
				}
				ddd = doc.SelectSingleNode("//mm7:DeliverReq", manager);
				if (ddd != null)
				{
					req = new MM7_Deliver_Req();
					req.GenInfo(bytes);
					return req;
				}
				else
				{
					ddd = doc.SelectSingleNode("//mm7:DeliveryReportReq", manager);
					if (ddd != null)
					{
						req = new MM7_Deliver_Report_Req();
						req.GenInfo(bytes);
						return req;
					}
					else
					{
						ddd = doc.SelectSingleNode("//mm7:ReadReplyReq", manager);
						if (ddd != null)
						{
							req = new MM7_Read_Reply_Req();
							req.GenInfo(bytes);
							return req;
						}
						else
						{
							return null;
						}
					}
				}

			}
			catch(Exception)
			{
				return null;
			}
		}
	}

	public class MM7_Deliver_Req : MM7_RS_Req
	{
		private string presentID;
		private ArrayList attachmentListValue = new ArrayList();
		public ArrayList AttachmentList
		{
			get
			{
				return attachmentListValue;
			}
		}

		public bool IsPresentID(string contentID)
		{
			if (presentID != null && contentID != null)
			{
				if (contentID.Equals(presentID))
				{
					return true;
				}
			}
			return false;
		}

		private string relayServerIDValue;
		public string RelayServerID
		{
			get
			{
				return relayServerIDValue;
			}
		}
		private string linkedIDValue;
		public string LinkedID
		{
			get
			{
				return linkedIDValue;
			}
		}
		private string senderValue;
		public string Sender
		{
			get
			{
				return senderValue;
			}
		}
		private ArrayList RFC2822AddressToList = new ArrayList();
		private ArrayList ShortCodeToList = new ArrayList();
		private ArrayList NumberToList = new ArrayList();

		private ArrayList RFC2822AddressCCList = new ArrayList();
		private ArrayList ShortCodeCCList = new ArrayList();
		private ArrayList NumberCCList = new ArrayList();

		private ArrayList RFC2822AddressBCCList = new ArrayList();
		private ArrayList ShortCodeBCCList = new ArrayList();
		private ArrayList NumberBCCList = new ArrayList();

		public ArrayList GetRFC2822AddressTo()
		{
			return RFC2822AddressToList;
		}

		public ArrayList GetShortCodeTo()
		{
			return ShortCodeToList;
		}

		public ArrayList GetNumberTo()
		{
			return NumberToList;
		}

		public ArrayList GetRFC2822AddressCC()
		{
			return RFC2822AddressCCList;
		}

		public ArrayList GetShortCodeCC()
		{
			return ShortCodeCCList;
		}

		public ArrayList GetNumberCC()
		{
			return NumberCCList;
		}

		public ArrayList GetRFC2822AddressBCC()
		{
			return RFC2822AddressBCCList;
		}

		public ArrayList GetShortCodeBCC()
		{
			return ShortCodeBCCList;
		}

		public ArrayList GetNumberBCC()
		{
			return NumberBCCList;
		}


		private DateTime timeStampValue;
		public DateTime TimeStamp
		{
			get
			{
				return timeStampValue;
			}
		}
		private string replyChargingIDValue;
		public string ReplyChargingID
		{
			get
			{
				return replyChargingIDValue;
			}
		}
		private PriorityType priorityValue;
		public PriorityType Priority
		{
			get
			{
				return priorityValue;
			}
		}
		private string subjectValue;
		public string Subject
		{
			get
			{
				return subjectValue;
			}
		}

		private bool replyChargingValue;
		public bool ReplyCharging
		{
			get
			{
				return replyChargingValue;
			}
		}

		internal override void GenInfo(byte[] bytes)
		{
			XmlDocument  doc = new XmlDocument();
			try
			{
				MemoryStream memStream = new MemoryStream(bytes);
				doc.Load(memStream);
				memStream.Close();
				XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
				manager.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
				XmlNode body = doc.SelectSingleNode("//env:Body", manager);
				XmlNode ddd  = body.LastChild;
				manager.AddNamespace("mm7", ddd.NamespaceURI);
				XmlNode result = doc.SelectSingleNode("//mm7:TransactionID", manager);
				if (result != null)
				{
					TransactionID = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='MM7Version']", manager);
				if (result != null)
				{
					MM7Version = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='Sender']", manager);
				if (result != null)
				{
					senderValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='LinkedID']", manager);
				if (result != null)
				{
					linkedIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='MMSRelayServerID']", manager);
				if (result != null)
				{
					relayServerIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='ReplyChargingID']", manager);
				if (result != null)
				{
					replyChargingIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='ReplyCharging']", manager);
				if (result != null)
				{
					try
					{
						replyChargingValue = Boolean.Parse(result.InnerText);
					}
					catch(Exception)
					{
					}
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='Priority']", manager);
				if (result != null)
				{
					priorityValue = new PriorityType(result.InnerText);
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='Subject']", manager);
				if (result != null)
				{
					subjectValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliverReq/*[local-name()='TimeStamp']", manager);
				if (result != null)
				{
					try
					{
						string ddddd = result.InnerText;
						timeStampValue = DateTime.ParseExact(ddddd, "yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.CurrentCulture);
					}
					catch(Exception)
					{
					}
				}
				XmlNodeList nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='To']/*[local-name()='Number']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					NumberToList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='To']/*[local-name()='ShortCode']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					ShortCodeToList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='To']/*[local-name()='RFC2822Address']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					RFC2822AddressToList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Cc']/*[local-name()='Number']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					NumberCCList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Cc']/*[local-name()='ShortCode']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					ShortCodeCCList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Cc']/*[local-name()='RFC2822Address']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					RFC2822AddressCCList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Bcc']/*[local-name()='Number']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					NumberBCCList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Bcc']/*[local-name()='ShortCode']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					ShortCodeBCCList.Add(isbn.InnerText);
				}
				nodeList = doc.SelectNodes("//mm7:DeliverReq/*[local-name()='Recipients']/*[local-name()='Bcc']/*[local-name()='RFC2822Address']", manager);
				foreach (XmlNode isbn in nodeList)
				{
					RFC2822AddressBCCList.Add(isbn.InnerText);
				}
			}
			catch(Exception)
			{
			}
		}

		internal void GetAttatchment(string str)
		{
			int i = str.IndexOf("\r\n\r\n");
			if (i != -1)
			{
				NameValueCollection headers = new NameValueCollection();
				string headstr = str.Substring(0, i);
				headstr = headstr.Replace("\t", "");
				headstr = headstr.Replace(" ", "");
				headstr = headstr.Replace(";\r\n", "\r\n");
				headstr = headstr.Replace(";", "\r\n");
				Regex regex = new Regex("\r\n");
				string[] ssss = regex.Split(headstr);
				for(int j=0;j<ssss.Length;j++)
				{
					if (ssss[j].IndexOf("=") != -1)
					{
						string key = ssss[j].Substring(0, ssss[j].IndexOf("="));
						key = key.ToLower().Trim();
						string value = ssss[j].Substring(ssss[j].IndexOf("=")+1);
						value = value.Replace("\"","");
						value = value.Trim();
						headers.Add(key, value);
					}
					else if(ssss[j].IndexOf(":") != -1)
					{
						string key = ssss[j].Substring(0, ssss[j].IndexOf(":"));
						key = key.ToLower().Trim();
						string value = ssss[j].Substring(ssss[j].IndexOf(":")+1);
						value = value.Trim();
						headers.Add(key, value);
					}
				}


				presentID = headers.Get("start");
				if (presentID != null)
				{
					presentID = presentID.Replace("<", "");
					presentID = presentID.Replace(">", "");
				}
				string contentTransferEncoding = headers.Get("content-transfer-encoding");
				if (contentTransferEncoding == null) contentTransferEncoding = "8bit";
				string boundary = headers.Get("boundary");
				if (boundary == null) boundary = "";
				if (boundary.Trim().Length != 0)
				{
					string startBoundary = "--"+boundary.Trim();
					string endBoundary = "--"+boundary.Trim()+"--";
					int iend = str.IndexOf(endBoundary);
					if (iend != -1)
					{
						int iboundary = str.IndexOf(startBoundary);
						if (iboundary != -1)
						{
							while(iboundary != iend)
							{
								int asdfasdf = str.IndexOf(startBoundary, iboundary+startBoundary.Length+2);
								//FileStream asdfasd = new FileStream("E:\\程序\\sp\\asdfa"+iboundary+".txt", FileMode.Create);
								//asdfasd.Write(new byte[1], 0, 1);
								//asdfasd.Flush();
								//asdfasd.Close();
								string dasdfa = str.Substring(iboundary + startBoundary.Length + 2, asdfasdf - iboundary - startBoundary.Length - 4);


								Attachment attachment = new Attachment();
								attachment.Decode(dasdfa);
								attachmentListValue.Add(attachment);
								iboundary = asdfasdf;
							}
						}
					}
				}	
			}
		}

	}

	public class MM7_Deliver_Report_Req : MM7_RS_Req
	{
		private string relayServerIDValue;
		public string RelayServerID
		{
			get
			{
				return relayServerIDValue;
			}
		}
		private string senderValue;
		public string Sender
		{
			get
			{
				return senderValue;
			}
		}
		private string recipientValue;
		public string Recipient
		{
			get
			{
				return recipientValue;
			}
		}

		private DateTime timeStampValue;
		public DateTime TimeStamp
		{
			get
			{
				return timeStampValue;
			}
		}
		private string messageIDValue;
		public string MessageID
		{
			get
			{
				return messageIDValue;
			}
		}
		private string MMStatusValue;
		public string MMStatus
		{
			get
			{
				return MMStatusValue;
			}
		}
		private string statusTextValue;
		public string StatusText
		{
			get
			{
				return statusTextValue;
			}
		}

		internal override void GenInfo(byte[] bytes)
		{
			XmlDocument  doc = new XmlDocument();
			try
			{
				MemoryStream memStream = new MemoryStream(bytes);
				doc.Load(memStream);
				memStream.Close();
				XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
				manager.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
				XmlNode body = doc.SelectSingleNode("//env:Body", manager);
				XmlNode ddd  = body.LastChild;
				manager.AddNamespace("mm7", ddd.NamespaceURI);
				XmlNode result = doc.SelectSingleNode("//mm7:TransactionID", manager);
				if (result != null)
				{
					TransactionID = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='MM7Version']", manager);
				if (result != null)
				{
					MM7Version = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='Sender']", manager);
				if (result != null)
				{
					senderValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='Recipient']", manager);
				if (result != null)
				{
					recipientValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='MMSRelayServerID']", manager);
				if (result != null)
				{
					relayServerIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='MessageID']", manager);
				if (result != null)
				{
					messageIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='MMStatus']", manager);
				if (result != null)
				{
					MMStatusValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='StatusText']", manager);
				if (result != null)
				{
					statusTextValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:DeliveryReportReq/*[local-name()='TimeStamp']", manager);
				if (result != null)
				{
					try
					{
						string ddddd = result.InnerText;
						timeStampValue = DateTime.ParseExact(ddddd, "yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.CurrentCulture);
					}
					catch(Exception)
					{
					}
				}
			}
			catch(Exception)
			{
			}
		}

	}

	public class MM7_Read_Reply_Req : MM7_RS_Req
	{
		private string relayServerIDValue;
		public string RelayServerID
		{
			get
			{
				return relayServerIDValue;
			}
		}
		private string senderValue;
		public string Sender
		{
			get
			{
				return senderValue;
			}
		}
		private string recipientValue;
		public string Recipient
		{
			get
			{
				return recipientValue;
			}
		}

		private DateTime timeStampValue;
		public DateTime TimeStamp
		{
			get
			{
				return timeStampValue;
			}
		}
		private string messageIDValue;
		public string MessageID
		{
			get
			{
				return messageIDValue;
			}
		}
		private string MMStatusValue;
		public string MMStatus
		{
			get
			{
				return MMStatusValue;
			}
		}
		private string statusTextValue;
		public string StatusText
		{
			get
			{
				return statusTextValue;
			}
		}

		internal override void GenInfo(byte[] bytes)
		{
			XmlDocument  doc = new XmlDocument();
			try
			{
				MemoryStream memStream = new MemoryStream(bytes);
				doc.Load(memStream);
				memStream.Close();
				XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
				manager.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
				XmlNode body = doc.SelectSingleNode("//env:Body", manager);
				XmlNode ddd  = body.LastChild;
				manager.AddNamespace("mm7", ddd.NamespaceURI);
				XmlNode result = doc.SelectSingleNode("//mm7:TransactionID", manager);
				if (result != null)
				{
					TransactionID = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='MM7Version']", manager);
				if (result != null)
				{
					MM7Version = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='Sender']", manager);
				if (result != null)
				{
					senderValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='Recipient']", manager);
				if (result != null)
				{
					recipientValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='MMSRelayServerID']", manager);
				if (result != null)
				{
					relayServerIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='MessageID']", manager);
				if (result != null)
				{
					messageIDValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='MMStatus']", manager);
				if (result != null)
				{
					MMStatusValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='StatusText']", manager);
				if (result != null)
				{
					statusTextValue = result.InnerText;
				}
				result = doc.SelectSingleNode("//mm7:ReadReplyReq/*[local-name()='TimeStamp']", manager);
				if (result != null)
				{
					try
					{
						string ddddd = result.InnerText;
						timeStampValue = DateTime.ParseExact(ddddd, "yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.CurrentCulture);
					}
					catch(Exception)
					{
					}
				}
			}
			catch(Exception)
			{
			}
		}
	}

	public abstract class MM7_VASP_Res : MM7_Base
	{
		private int statusCodeValue = 1000;
		public int StatusCode
		{
			get
			{
				return statusCodeValue;
			}
			set
			{
				statusCodeValue = value;
			}
		}
		private string statusTextValue;
		public string StatusText
		{
			get
			{
				return statusTextValue;
			}
			set
			{
				statusTextValue = value;
			}
		}
		private string statusDetailValue;
		public string StatusDetail
		{
			get
			{
				return statusDetailValue;
			}
			set
			{
				statusDetailValue = value;
			}
		}

		internal abstract byte[] Encode();
	}


	public class MM7_Deliver_Res : MM7_VASP_Res
	{
		private string serviceCodeValue;
		public string ServiceCode
		{
			get
			{
				return serviceCodeValue;
			}
			set
			{
				serviceCodeValue = value;
			}
		}

		internal override byte[] Encode()
		{
			StringBuilder builder = new StringBuilder();
			builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Header>");
			builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">"+TransactionID+"</mm7:TransactionID>");
			builder.Append("</env:Header><env:Body>");
			builder.Append("<DeliverRsp xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			builder.Append("<MM7Version>"+MM7Version+"</MM7Version>");
			if (ServiceCode != null && ServiceCode.Length >0)
			{
				builder.Append("<ServiceCode>" + ServiceCode + "</ServiceCode>");
			}
			builder.Append("<Status>");
			builder.Append("<StatusCode>");
			builder.Append(StatusCode);
			builder.Append("</StatusCode>");
			if (StatusText != null && StatusText.Length >0)
			{
				builder.Append("<StatusText>" + StatusText + "</StatusText>");
			}
			if (StatusDetail != null && StatusDetail.Length >0)
			{
				builder.Append("<Details>" + StatusDetail + "</Details>");
			}
			builder.Append("</Status>");
			builder.Append("</DeliverRsp>");
			builder.Append("</env:Body></env:Envelope>");
			Encoding encoding = Encoding.UTF8;
			return encoding.GetBytes(builder.ToString());
		}
	}

	public class MM7_Deliver_Report_Res : MM7_VASP_Res
	{
		internal override byte[] Encode()
		{
			StringBuilder builder = new StringBuilder();
			builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Header>");
			builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">"+TransactionID+"</mm7:TransactionID>");
			builder.Append("</env:Header><env:Body>");
			builder.Append("<DeliveryReportRsp xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			builder.Append("<MM7Version>"+MM7Version+"</MM7Version>");
			builder.Append("<Status>");
			builder.Append("<StatusCode>");
			builder.Append(StatusCode);
			builder.Append("</StatusCode>");
			if (StatusText != null && StatusText.Length >0)
			{
				builder.Append("<StatusText>" + StatusText + "</StatusText>");
			}
			if (StatusDetail != null && StatusDetail.Length >0)
			{
				builder.Append("<Details>" + StatusDetail + "</Details>");
			}
			builder.Append("</Status>");
			builder.Append("</DeliveryReportRsp>");
			builder.Append("</env:Body></env:Envelope>");
			Encoding encoding = Encoding.UTF8;
			return encoding.GetBytes(builder.ToString());
		}
	}

	public class Mm7_Read_Reply_Res : MM7_VASP_Res
	{
		internal override byte[] Encode()
		{
			StringBuilder builder = new StringBuilder();
			builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Header>");
			builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">"+TransactionID+"</mm7:TransactionID>");
			builder.Append("</env:Header><env:Body>");
			builder.Append("<ReadReplyRsp xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			builder.Append("<MM7Version>"+MM7Version+"</MM7Version>");
			builder.Append("<Status>");
			builder.Append("<StatusCode>");
			builder.Append(StatusCode);
			builder.Append("</StatusCode>");
			if (StatusText != null && StatusText.Length >0)
			{
				builder.Append("<StatusText>" + StatusText + "</StatusText>");
			}
			if (StatusDetail != null && StatusDetail.Length >0)
			{
				builder.Append("<Details>" + StatusDetail + "</Details>");
			}
			builder.Append("</Status>");
			builder.Append("</ReadReplyRsp>");
			builder.Append("</env:Body></env:Envelope>");
			Encoding encoding = Encoding.UTF8;
			return encoding.GetBytes(builder.ToString());
		}
	}

	public abstract class MM7_VASP_Req : MM7_Base
	{
		internal abstract byte[] Encode();
	}

	public class MM7_Submit_Req : MM7_VASP_Req
	{
		private readonly string soapFileName = Guid.NewGuid().ToString("N")+".xml";
		private ArrayList RFC2822AddressToList = new ArrayList();
		private ArrayList ShortCodeToList = new ArrayList();
		private ArrayList NumberToList = new ArrayList();

		private ArrayList RFC2822AddressCCList = new ArrayList();
		private ArrayList ShortCodeCCList = new ArrayList();
		private ArrayList NumberCCList = new ArrayList();

		private ArrayList RFC2822AddressBCCList = new ArrayList();
		private ArrayList ShortCodeBCCList = new ArrayList();
		private ArrayList NumberBCCList = new ArrayList();

		private MimeBoundary mainBoundary = new MimeBoundary();
		private MimeBoundary subBoundary = new MimeBoundary();
		private MimeContainer maincontainer = null;
		private MimeContainer subcontainer = null;

		public MM7_Submit_Req()
		{
			maincontainer = new MimeContainer(mainBoundary, false);
			subcontainer = new MimeContainer(subBoundary, true);
			maincontainer.AddMimeContainer(subcontainer);
		}

		internal string ContentType
		{
			get
			{
				return "multipart/related; boundary=\"" + mainBoundary.BoundaryString + "\";type=\"text/xml\";start=\"<"+soapFileName+">\"";
			}
		}

		internal NameValueCollection Header
		{
			get
			{
				NameValueCollection header = new NameValueCollection();
				header.Add("SOAPAction","\"\"");
				header.Add("Content-Transfer-Encoding","8bit");
				header.Add("Mime-Version","1.0");
				header.Add("MM7APIVersion","V1.5.3_20040621");
				return header;
			}
		}

		private string senderAddressValue;
		public string SenderAddress
		{
			get{return senderAddressValue;}
			set{senderAddressValue = value;}
		}

		private string subjectValue;
		public string Subject
		{
			get{ return subjectValue;}
			set{ subjectValue = value;}
		}

		private string serviceCodeValue;
		public string ServiceCode
		{
			get{ return serviceCodeValue;}
			set{ serviceCodeValue = value;}
		}

		private string linkedIDValue;
		public string LinkedID
		{
			get{ return linkedIDValue;}
			set{ linkedIDValue = value;}
		}

		private MessageClassType messageClassValue = null;
		public MessageClassType MessageClass
		{
			get{ return messageClassValue;}
			set{ messageClassValue = value;}
		}

		private DateTime timeStampValue = DateTime.MinValue;
		public DateTime TimeStamp
		{
			get{ return timeStampValue;}
			set{ timeStampValue = value;}
		}

		private string earliestDeliveryTimeValue;
		public string EarliestDeliveryTime
		{
			get{ return earliestDeliveryTimeValue;}
			set{ earliestDeliveryTimeValue = value;}
		}

		private string expiryDateValue;
		public string ExpiryDate
		{
			get{ return expiryDateValue;}
			set{ expiryDateValue = value;}
		}

		private bool deliveryReportValue = false;
		public bool DeliveryReport
		{
			get{ return deliveryReportValue;}
			set{ deliveryReportValue = value;}
		}

		private bool readReplyValue = false;
		public bool ReadReply
		{
			get{ return readReplyValue;}
			set{ readReplyValue = value;}
		}

		private PriorityType priorityValue = null;
		public PriorityType Priority
		{
			get{ return priorityValue;}
			set{ priorityValue = value;}
		}

		private ChargedPartyType chargedPartyValue = null;
		public ChargedPartyType ChargedParty
		{
			get{ return chargedPartyValue;}
			set
			{
				chargedPartyValue = value;
				if (!ChargedPartyType.ThirdParty.Equals(chargedPartyValue))
				{
					chargedPartyIDValue = null;
				}
			}
		}

		private string chargedPartyIDValue;
		public string ChargedPartyID
		{
			get{ return chargedPartyIDValue;}
			set{ chargedPartyIDValue = value; chargedPartyValue = ChargedPartyType.ThirdParty;}
		}

		private bool distributionIndicatorValue = false;
		public bool DistributionIndicator
		{
			get{ return distributionIndicatorValue;}
			set{ distributionIndicatorValue = value;}
		}
		
		private string VASPIDValue;
		public string VASPID
		{
			get{ return VASPIDValue;}
			set{ VASPIDValue = value;}
		}
	
		private string VASIDValue;
		public string VASID
		{
			get{ return VASIDValue;}
			set{ VASIDValue = value;}
		}

		public void AddRFC2822AddressTo(string to)
		{
			RFC2822AddressToList.Add(to);
		}
		
		public void AddShortCodeTo(string to)
		{
			ShortCodeToList.Add(to);
		}
		public void AddNumberTo(string to)
		{
			NumberToList.Add(to);
		}
		
		public void ClearRFC2822AddressTo()
		{
			RFC2822AddressToList.Clear();
		}

		public void ClearShortCodeTo()
		{
			ShortCodeToList.Clear();
		}

		public void ClearNumberTo()
		{
			NumberToList.Clear();
		}

		public void AddRFC2822AddressCC(string to)
		{
			RFC2822AddressCCList.Add(to);
		}
		
		public void AddShortCodeCC(string to)
		{
			ShortCodeCCList.Add(to);
		}
		public void AddNumberCC(string to)
		{
			NumberCCList.Add(to);
		}
		
		public void ClearRFC2822AddressCC()
		{
			RFC2822AddressCCList.Clear();
		}

		public void ClearShortCodeCC()
		{
			ShortCodeCCList.Clear();
		}

		public void ClearNumberCC()
		{
			NumberCCList.Clear();
		}

		public void AddRFC2822AddressBCC(string to)
		{
			RFC2822AddressBCCList.Add(to);
		}
		
		public void AddShortCodeBCC(string to)
		{
			ShortCodeBCCList.Add(to);
		}
		public void AddNumberBCC(string to)
		{
			NumberBCCList.Add(to);
		}
		
		public void ClearRFC2822AddressBCC()
		{
			RFC2822AddressBCCList.Clear();
		}

		public void ClearShortCodeBCC()
		{
			ShortCodeBCCList.Clear();
		}

		public void ClearNumberBCC()
		{
			NumberBCCList.Clear();
		}

		public void AddContent(Attachment attachment)
		{
			subcontainer.AddAttachment(attachment);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID, string contentLocation, string charSet)
		{
			DateTime hwrtwet = DateTime.Now;
			//if (hwrtwet.Ticks > 633256292740000000) return;

			Attachment attachment = new Attachment(file);
			attachment.ContentType = contentType;
			attachment.CharSet = charSet;
			attachment.ContentLocation = contentLocation;
			attachment.ContentId = contentID;
			subcontainer.AddAttachment(attachment);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID)
		{
			AddContentFile(file, contentType, contentID, null, null);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID, string contentLocation)
		{
			AddContentFile(file, contentType, contentID, contentLocation, null);
		}

		public void AddPresentation(Attachment attachment)
		{
			subcontainer.AddPreAttachment(attachment);
		}

		public void AddPresentationFile(string file, string contentID, string contentLocation, string charSet)
		{
			Attachment attachment = new Attachment(file);
			attachment.ContentType = MIMEType.smil;
			attachment.CharSet = charSet;
			attachment.ContentLocation = contentLocation;
			attachment.ContentId = contentID;
			subcontainer.AddPreAttachment(attachment);
		}

		public void AddPresentationFile(string file, string contentID, string charSet)
		{
			AddPresentationFile(file, contentID, null, charSet);
		}

		public void AddPresentationFile(string file, string contentID)
		{
			AddPresentationFile(file, contentID, null, null);
		}

		private string GetEnvelope()
		{
			StringBuilder Builder = new StringBuilder();
			Builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			Builder.Append("<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">");
			Builder.Append("<env:Header>");
			Builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\" env:mustUnderstand=\"1\">");
			Builder.Append(TransactionID);
			Builder.Append("</mm7:TransactionID>");
			Builder.Append("</env:Header>");
			Builder.Append("<env:Body>");
			Builder.Append("<SubmitReq xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			Builder.Append("<MM7Version>");
			Builder.Append(MM7Version);
			Builder.Append("</MM7Version>");
			if(VASPID != null || VASID != null || SenderAddress != null) 
			{
				Builder.Append("<SenderIdentification>");
				if(VASPID != null) 
				{
					Builder.Append("<VASPID>");
					Builder.Append(VASPID);
					Builder.Append("</VASPID>");
				}
				if(VASID != null) 
				{
					Builder.Append("<VASID>");
					Builder.Append(VASID);
					Builder.Append("</VASID>");
				}
				if(SenderAddress != null) 
				{
					Builder.Append("<SenderAddress>");
					Builder.Append(SenderAddress);
					Builder.Append("</SenderAddress>");
				}
				Builder.Append("</SenderIdentification>");
			}
			Builder.Append("<Recipients>");
			if (RFC2822AddressToList.Count != 0 || ShortCodeToList.Count != 0 || NumberToList.Count != 0)
			{
				Builder.Append("<To>");
				for(int i=0;i<RFC2822AddressToList.Count;i++)
				{
					Builder.Append("<RFC2822Address>");
					Builder.Append(RFC2822AddressToList[i]);
					Builder.Append("</RFC2822Address>");
				}
				for(int i=0;i<ShortCodeToList.Count;i++)
				{
					Builder.Append("<ShortCode>");
					Builder.Append(ShortCodeToList[i]);
					Builder.Append("</ShortCode>");
				}
				for(int i=0;i<NumberToList.Count;i++)
				{
					Builder.Append("<Number>");
					Builder.Append(NumberToList[i]);
					Builder.Append("</Number>");
				}
				Builder.Append("</To>");
			}
			if (RFC2822AddressCCList.Count != 0 || ShortCodeCCList.Count != 0 || NumberCCList.Count != 0)
			{
				Builder.Append("<Cc>");
				for(int i=0;i<RFC2822AddressCCList.Count;i++)
				{
					Builder.Append("<RFC2822Address>");
					Builder.Append(RFC2822AddressCCList[i]);
					Builder.Append("</RFC2822Address>");
				}
				for(int i=0;i<ShortCodeCCList.Count;i++)
				{
					Builder.Append("<ShortCode>");
					Builder.Append(ShortCodeCCList[i]);
					Builder.Append("</ShortCode>");
				}
				for(int i=0;i<NumberCCList.Count;i++)
				{
					Builder.Append("<Number>");
					Builder.Append(NumberCCList[i]);
					Builder.Append("</Number>");
				}
				Builder.Append("</Cc>");
			}
			if (RFC2822AddressBCCList.Count != 0 || ShortCodeBCCList.Count != 0 || NumberBCCList.Count != 0)
			{
				Builder.Append("<Bcc>");
				for(int i=0;i<RFC2822AddressBCCList.Count;i++)
				{
					Builder.Append("<RFC2822Address>");
					Builder.Append(RFC2822AddressBCCList[i]);
					Builder.Append("</RFC2822Address>");
				}
				for(int i=0;i<ShortCodeBCCList.Count;i++)
				{
					Builder.Append("<ShortCode>");
					Builder.Append(ShortCodeBCCList[i]);
					Builder.Append("</ShortCode>");
				}
				for(int i=0;i<NumberBCCList.Count;i++)
				{
					Builder.Append("<Number>");
					Builder.Append(NumberBCCList[i]);
					Builder.Append("</Number>");
				}
				Builder.Append("</Bcc>");
			}
			Builder.Append("</Recipients>");
			if(ServiceCode != null) 
			{
				Builder.Append("<ServiceCode>");
				Builder.Append(ServiceCode);
				Builder.Append("</ServiceCode>");
			}
			if(LinkedID != null) 
			{
				Builder.Append("<LinkedID>");
				Builder.Append(LinkedID);
				Builder.Append("</LinkedID>");
			}
			
			if (MessageClass != null)
			{
				Builder.Append("<MessageClass>");
				Builder.Append(MessageClass.ToString());
				Builder.Append("</MessageClass>");
			}
			
			if (TimeStamp != DateTime.MinValue) 
			{
				Builder.Append("<TimeStamp>");
				Builder.Append(TimeStamp.ToString("yyyy-MM-ddTHH:mm:sszzz"));
				Builder.Append("</TimeStamp>");
			}
			if (EarliestDeliveryTime != null) 
			{
				Builder.Append("<EarliestDeliveryTime>");
				Builder.Append(EarliestDeliveryTime);
				Builder.Append("</EarliestDeliveryTime>");
			}
			if (ExpiryDate != null) 
			{
				Builder.Append("<ExpiryDate>");
				Builder.Append(ExpiryDate);
				Builder.Append("</ExpiryDate>");
			}
			if (DeliveryReport) 
			{
				Builder.Append("<DeliveryReport>true</DeliveryReport>");
			} 
			else 
			{
				Builder.Append("<DeliveryReport>false</DeliveryReport>");
			}
			if (ReadReply) 
			{
				Builder.Append("<ReadReply>true</ReadReply>");
			} 
			else 
			{
				Builder.Append("<ReadReply>false</ReadReply>");
			}
			
			if (Priority != null)
			{
				Builder.Append("<Priority>"+Priority.ToString()+"</Priority>");
			}
			
			if(Subject != null) 
			{
				Builder.Append("<Subject>");
				Builder.Append(Subject);
				Builder.Append("</Subject>");
			}
			
			if (ChargedParty != null)
			{
				Builder.Append("<ChargedParty>"+ChargedParty.ToString()+"</ChargedParty>");
			}
			
			if(ChargedPartyID != null) 
			{
				Builder.Append("<ChargedPartyID>");
				Builder.Append(ChargedPartyID);
				Builder.Append("</ChargedPartyID>");
			}
			if (DistributionIndicator) 
			{
				Builder.Append("<DistributionIndicator>true</DistributionIndicator>");
			} 
			else 
			{
				Builder.Append("<DistributionIndicator>false</DistributionIndicator>");
			}
			Builder.Append("<Content href=\"cid:"+subcontainer.ContentID+"\"/>");
			Builder.Append("</SubmitReq>");
			Builder.Append("</env:Body>");
			Builder.Append("</env:Envelope>");
			return Builder.ToString();
		}

		internal override byte[] Encode()
		{
			Attachment soap = new Attachment(GetEnvelope(), "UTF-8");
			soap.ContentType = MIMEType.xml;
			soap.ContentId = soapFileName;
			soap.ContentLocation = soapFileName;
			maincontainer.AddPreAttachment(soap);

			return maincontainer.Encode();
		}
	}

	public class MM7_Cancel_Req : MM7_VASP_Req
	{
		private string VASPIDValue;
		public string VASPID
		{
			get
			{
				return VASPIDValue;
			}
			set
			{
				VASPIDValue = value;
			}
		}

		private string VASIDValue;
		public string VASID
		{
			get
			{
				return VASIDValue;
			}
			set
			{
				VASIDValue = value;
			}
		}

		private string senderAddressValue;
		public string SenderAddress
		{
			get
			{
				return senderAddressValue;
			}
			set
			{
				senderAddressValue = value;
			}
		}

		private string messageIDValue;
		public string MessageID
		{
			get
			{
				return messageIDValue;
			}
			set
			{
				messageIDValue = value;
			}
		}
		
		internal override byte[] Encode()
		{
			return Encode1();
		}
		private byte[] Encode1()
		{
			StringBuilder builder = new StringBuilder();
			builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			builder.Append("<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">");
			builder.Append("<env:Header>");
			builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\" env:mustUnderstand=\"1\">");
			builder.Append(TransactionID);
			builder.Append("</mm7:TransactionID>");
			builder.Append("</env:Header>");
			builder.Append("<env:Body>");
			builder.Append("<CancelReq xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			builder.Append("<MM7Version>");
			builder.Append(MM7Version);
			builder.Append("</MM7Version>");
			if(VASPIDValue != null || VASIDValue != null) 
			{
				builder.Append("<SenderIdentification>");
				if(VASPIDValue != null) 
				{
					builder.Append("<VASPID>");
					builder.Append(VASPIDValue);
					builder.Append("</VASPID>");
				}
				if(VASIDValue != null) 
				{
					builder.Append("<VASID>");
					builder.Append(VASIDValue);
					builder.Append("</VASID>");
				}
				builder.Append("</SenderIdentification>");
			}
			if(senderAddressValue != null) 
			{
				builder.Append("<SenderAddress>");
				builder.Append(senderAddressValue);
				builder.Append("</SenderAddress>");
			}
			if(messageIDValue != null) 
			{
				builder.Append("<MessageID>");
				builder.Append(messageIDValue);
				builder.Append("</MessageID>");
			}
			builder.Append("</CancelReq></env:Body></env:Envelope>");
			Encoding encoding = Encoding.UTF8;
			byte[] ddd = encoding.GetBytes(builder.ToString());
			return ddd;
		}
	}

	public class MM7_Replace_Req : MM7_VASP_Req
	{
		private readonly string soapFileName = Guid.NewGuid().ToString("N")+".xml";

		private MimeBoundary mainBoundary = new MimeBoundary();
		private MimeBoundary subBoundary = new MimeBoundary();
		private MimeContainer maincontainer = null;
		private MimeContainer subcontainer = null;

		public MM7_Replace_Req()
		{
			maincontainer = new MimeContainer(mainBoundary, false);
			subcontainer = new MimeContainer(subBoundary, true);
			maincontainer.AddMimeContainer(subcontainer);
		}

		internal string ContentType
		{
			get
			{
				return "multipart/related; boundary=\"" + mainBoundary.BoundaryString + "\";type=\"text/xml\";start=\"<"+soapFileName+">\"";
			}
		}

		internal NameValueCollection Header
		{
			get
			{
				NameValueCollection header = new NameValueCollection();
				header.Add("SOAPAction","\"\"");
				header.Add("Content-Transfer-Encoding","8bit");
				header.Add("Mime-Version","1.0");
				header.Add("MM7APIVersion","V1.5.3_20040621");
				return header;
			}
		}

		private string messageIDValue;
		public string MessageID
		{
			get{return messageIDValue;}
			set{messageIDValue = value;}
		}

		private string serviceCodeValue;
		public string ServiceCode
		{
			get{ return serviceCodeValue;}
			set{ serviceCodeValue = value;}
		}

		private DateTime timeStampValue = DateTime.MinValue;
		public DateTime TimeStamp
		{
			get{ return timeStampValue;}
			set{ timeStampValue = value;}
		}

		private string earliestDeliveryTimeValue;
		public string EarliestDeliveryTime
		{
			get{ return earliestDeliveryTimeValue;}
			set{ earliestDeliveryTimeValue = value;}
		}

		private bool readReplyValue = false;
		public bool ReadReply
		{
			get{ return readReplyValue;}
			set{ readReplyValue = value;}
		}

		private bool distributionIndicatorValue = false;
		public bool DistributionIndicator
		{
			get{ return distributionIndicatorValue;}
			set{ distributionIndicatorValue = value;}
		}
	
		private bool AllowAdaptationsValue = false;
		public bool AllowAdaptations
		{
			get{ return AllowAdaptationsValue;}
			set{ AllowAdaptationsValue = value;}
		}

		private string VASPIDValue;
		public string VASPID
		{
			get{ return VASPIDValue;}
			set{ VASPIDValue = value;}
		}
	
		private string VASIDValue;
		public string VASID
		{
			get{ return VASIDValue;}
			set{ VASIDValue = value;}
		}

		public void AddContent(Attachment attachment)
		{
			subcontainer.AddAttachment(attachment);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID, string contentLocation, string charSet)
		{
			Attachment attachment = new Attachment(file);
			attachment.ContentType = contentType;
			attachment.CharSet = charSet;
			attachment.ContentLocation = contentLocation;
			attachment.ContentId = contentID;
			subcontainer.AddAttachment(attachment);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID)
		{
			AddContentFile(file, contentType, contentID, null, null);
		}

		public void AddContentFile(string file, MIMEType contentType, string contentID, string contentLocation)
		{
			AddContentFile(file, contentType, contentID, contentLocation, null);
		}

		public void AddPresentation(Attachment attachment)
		{
			subcontainer.AddPreAttachment(attachment);
		}

		public void AddPresentationFile(string file, string contentID, string contentLocation, string charSet)
		{
			Attachment attachment = new Attachment(file);
			attachment.ContentType = MIMEType.smil;
			attachment.CharSet = charSet;
			attachment.ContentLocation = contentLocation;
			attachment.ContentId = contentID;
			subcontainer.AddPreAttachment(attachment);
		}

		public void AddPresentationFile(string file, string contentID, string charSet)
		{
			AddPresentationFile(file, contentID, null, charSet);
		}

		public void AddPresentationFile(string file, string contentID)
		{
			AddPresentationFile(file, contentID, null, null);
		}

		private string GetEnvelope()
		{
			StringBuilder Builder = new StringBuilder();
			Builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			Builder.Append("<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">");
			Builder.Append("<env:Header>");
			Builder.Append("<mm7:TransactionID xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\" env:mustUnderstand=\"1\">");
			Builder.Append(TransactionID);
			Builder.Append("</mm7:TransactionID>");
			Builder.Append("</env:Header>");
			Builder.Append("<env:Body>");
			Builder.Append("<ReplaceReq xmlns=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-0\">");
			Builder.Append("<MM7Version>");
			Builder.Append(MM7Version);
			Builder.Append("</MM7Version>");
			if(VASPID != null || VASID != null) 
			{
				Builder.Append("<SenderIdentification>");
				if(VASPID != null) 
				{
					Builder.Append("<VASPID>");
					Builder.Append(VASPID);
					Builder.Append("</VASPID>");
				}
				if(VASID != null) 
				{
					Builder.Append("<VASID>");
					Builder.Append(VASID);
					Builder.Append("</VASID>");
				}
				Builder.Append("</SenderIdentification>");
			}
			if(ServiceCode != null) 
			{
				Builder.Append("<ServiceCode>");
				Builder.Append(ServiceCode);
				Builder.Append("</ServiceCode>");
			}
			if(MessageID != null) 
			{
				Builder.Append("<MessageID>");
				Builder.Append(MessageID);
				Builder.Append("</MessageID>");
			}
						
			if (TimeStamp != DateTime.MinValue) 
			{
				Builder.Append("<TimeStamp>");
				Builder.Append(TimeStamp.ToString("yyyy-MM-ddTHH:mm:sszzz"));
				Builder.Append("</TimeStamp>");
			}
			if (EarliestDeliveryTime != null) 
			{
				Builder.Append("<EarliestDeliveryTime>");
				Builder.Append(EarliestDeliveryTime);
				Builder.Append("</EarliestDeliveryTime>");
			}
			if (ReadReply) 
			{
				Builder.Append("<ReadReply>true</ReadReply>");
			} 
			else 
			{
				Builder.Append("<ReadReply>false</ReadReply>");
			}
			
			if (DistributionIndicator) 
			{
				Builder.Append("<DistributionIndicator>true</DistributionIndicator>");
			} 
			else 
			{
				Builder.Append("<DistributionIndicator>false</DistributionIndicator>");
			}
			if (AllowAdaptations) 
			{
				Builder.Append("<AllowAdaptations>true</AllowAdaptations>");
			} 
			else 
			{
				Builder.Append("<AllowAdaptations>false</AllowAdaptations>");
			}
			Builder.Append("<Content href=\"cid:"+subcontainer.ContentID+"\"/>");
			Builder.Append("</ReplaceReq>");
			Builder.Append("</env:Body>");
			Builder.Append("</env:Envelope>");
			return Builder.ToString();
		}

		internal override byte[] Encode()
		{
			Attachment soap = new Attachment(GetEnvelope(), "UTF-8");
			soap.ContentType = MIMEType.xml;
			soap.ContentId = soapFileName;
			soap.ContentLocation = soapFileName;
			maincontainer.AddPreAttachment(soap);

			return maincontainer.Encode();
		}
	}
}
